﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using OF.Component.BLL;
using OF.Component.Model;
using OF.Component.Base;
using OF.Utility;
using OpSystem.manage;

namespace _2010web.Manage.authority
{
    public partial class AuthorityRoleList : OpBasePage
    {

        protected string strHtml = "";
        protected string strSystemName = "";
        protected int intSystemID = 0;
        protected string strStyle = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            SetPageCode(101, 10104);
            if (!Page.IsPostBack)
            {
                SystemBind();
                intSystemID = RequestUtility.GetQueryInt("sysid", 0);
                if (intSystemID > 0)
                {
                    AuthoritySystemInfo info = AuthoritySystemOperate.GetByID(intSystemID);
                    if (info != null)
                    {
                        strSystemName = info.SystemName ;
                    }
                    MyBind();
                    this.ddlSystem.SelectedValue = intSystemID.ToString();

                }
                else
                {
                    strStyle = "style='display:none'";
                }
            }
        }
        private void SystemBind()
        {

            int count = 0;
            string where = "1=1 and IsActive=1";
            IList<AuthoritySystemInfo> list = AuthoritySystemOperate.Search(out count, 1, 999, where, "SystemID", true);
            this.ddlSystem.DataSource = list;
            this.ddlSystem.DataTextField = "SystemName";
            this.ddlSystem.DataValueField = "SystemID";
            this.ddlSystem.DataBind();
            ddlSystem.Items.Insert(0, new ListItem("请选择系统", "0"));

        }

        private void MyBind()
        {

            int count = 0;
            intSystemID = RequestUtility.GetQueryInt("sysid", 1);
            IList<AuthorityRoleInfo> list = AuthorityRoleOperate.Search(out count, 1, 9999, "1=1 and IsActive=1 and SystemID=" + intSystemID, "RoleID", true);

            var ParentList = from AuthorityRole in list
                             where AuthorityRole.ParentID == 0
                             select new { RoleName = AuthorityRole.RoleName, RoleID = AuthorityRole.RoleID };


            StringBuilder sb = new StringBuilder();

            foreach (var pv in ParentList)
            {
                sb.AppendFormat("<tr><th><i class=\"bookmark\"></i>{0}(ID:{1})</th>", pv.RoleName, pv.RoleID);
                sb.AppendLine("<td><ul class=\"xg\">");
                sb.AppendFormat("<li onclick=\"_M.Include('AuthorityRoleEdit.aspx?cid={0}&sysid={1}', '添加子角色', 370, 175)\" ><span class=\"gou\">√</span>添加子项</li>", pv.RoleID, intSystemID);
                sb.AppendFormat("<li onclick=\"_M.Include('AuthorityRoleEdit.aspx?ID={0}&sysid={1}', '修改角色', 370, 175)\" ><span class=\"gou\">√</span>修改</li>", pv.RoleID, intSystemID);
                sb.AppendFormat("<li  onclick=\"_M.Include('AuthorityRoleMenuRelaEdit.aspx?id={0}&sysid={1}', '配置权限', 800, 500)\"><span class=\"gou\">√</span>配置权限</li>", pv.RoleID, intSystemID);
                sb.AppendFormat("<li onclick=\"$().AjaxManage.DeleteCheck('{0}', 'AuthorityRoleOperate');\" ><span class=\"gou\">√</span>删除</li>", pv.RoleID);
                sb.AppendLine("</ul>");
                sb.AppendLine("</td>");
                sb.AppendLine(" </tr>");

                var ChildList = from AuthorityRole in list
                                where AuthorityRole.ParentID == pv.RoleID
                                select new { RoleName = AuthorityRole.RoleName, RoleID = AuthorityRole.RoleID };

                foreach (var cv in ChildList)
                {
                    sb.AppendLine(" <tr>");
                    sb.AppendFormat("<td><i class=\"bookmark three\"></i>{0}（值：{1}）</td>", cv.RoleName, cv.RoleID);
                    sb.AppendLine("<td><ul class=\"xg\">");
                    sb.AppendFormat("<li  onclick=\"_M.Include('AuthorityRoleEdit.aspx?id={0}', '修改角色', 370, 175)\"><span class=\"gou\">√</span>修改</li>", cv.RoleID);
                    sb.AppendFormat("<li  onclick=\"_M.Include('AuthorityRoleMenuRelaEdit.aspx?id={0}&sysid={1}', '配置权限', 800, 500)\"><span class=\"gou\">√</span>配置权限</li>", cv.RoleID, intSystemID);
                    sb.AppendFormat("<li onclick=\"$().AjaxManage.DeleteCheck('{0}', 'AuthorityRoleOperate');\"><span class=\"gou\">√</span>删除</li>", cv.RoleID);
                    sb.AppendLine("</ul>");
                    sb.AppendLine("</td>");
                    sb.AppendLine("</tr>");

                }
            }
            strHtml = sb.ToString();

        }
    }
}
